<template>
  <div>
    <el-dialog
      class="my_dialog"
      v-bind="$attrs"
      v-on="$listeners"
      v-draggable
      width="600px"
      :visible="visible"
      @open="onOpen"
      @close="onClose"
      :title="title"
      :close-on-click-modal="false"
    >
      <el-form
        ref="elForm"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="100px"
      >
        <el-form-item label="仓库编码" prop="code">
          <el-input
            v-model="formData.code"
            placeholder="请输入仓库编码"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="仓库名称" prop="name">
          <el-input
            v-model="formData.name"
            placeholder="请输入仓库名称"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="仓库地址" prop="address">
          <el-input
            v-model="formData.address"
            placeholder="请输入仓库地址"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input
            v-model="formData.phone"
            placeholder="请输入联系电话"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="仓库位置" prop="location">
          <el-input
            v-model="formData.location"
            placeholder="请输入仓库位置"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="仓库类型" prop="wareTypeID">
          <el-select
            v-model="formData.wareTypeID"
            placeholder="请选择仓库类型"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in wareTypeIDOptions"
              :key="index"
              :label="item.dictLabel"
              :value="Number(item.dictValue)"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="仓库状态" prop="status">
          <el-select
            v-model="formData.status"
            placeholder="请选择仓库状态"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in statusOptions"
              :key="index"
              :label="item.dictLabel"
              :value="Number(item.dictValue)"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label-width="110px"
          label="ERP仓库编码"
          prop="eRPStockNumber"
        >
          <el-input
            v-model="formData.eRPStockNumber"
            placeholder="请输入仓库位置"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="所属部门" prop="deptId">
          <treeselect
            v-model="formData.deptId"
            :options="deptIdOptions"
            :show-count="true"
            placeholder="请选择归属部门"
          />
        </el-form-item>
        <el-form-item label="库存状态" prop="stockStatus">
          <el-select
            v-model="formData.stockStatus"
            placeholder="库存状态"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in stockStatusOptions"
              :key="index"
              :label="item.dictLabel"
              :value="Number(item.dictValue)"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否上架" prop="hSJFlag">
              <el-switch
                v-model="formData.hSJFlag"
                :active-value="0"
                :inactive-value="1"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否倒冲" prop="recoilFlag">
              <el-switch
                v-model="formData.recoilFlag"
                :active-value="0"
                :inactive-value="1"
              ></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label-width="135px"
              label="是否允许超量入库"
              prop="inFlag"
            >
              <el-switch
                v-model="formData.inFlag"
                :active-value="0"
                :inactive-value="1"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label-width="135px"
              label="是否先进先出"
              prop="hFIFOFlag"
            >
              <el-switch
                v-model="formData.hFIFOFlag"
                :active-value="0"
                :inactive-value="1"
              ></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label-width="135px"
              label="是否允许超量领用"
              prop="outFlag"
            >
              <el-switch
                v-model="formData.outFlag"
                :active-value="0"
                :inactive-value="1"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label-width="135px"
              label="是否参与APS计算"
              prop="aPSFlag"
            >
              <el-switch
                v-model="formData.aPSFlag"
                :active-value="0"
                :inactive-value="1"
              ></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="formData.remark"
            type="textarea"
            placeholder="请输入备注"
            :autosize="{ minRows: 4, maxRows: 4 }"
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="onClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { deptTreeSelect } from "@/api/system/user";
export default {
  inheritAttrs: false,
  components: { Treeselect },
  props: ["title", "visible", "item"],
  data() {
    return {
      formData: {
        code: "",
        name: "",
        address: "",
        phone: "",
        location: "",
        wareTypeID: "",
        status: "",
        eRPStockNumber: "",
        stockStatus: "",
        hSJFlag: 0,
        recoilFlag: 0,
        inFlag: 0,
        outFlag: 0,
        aPSFlag: 0,
        remark: "",
        hFIFOFlag: 0,
      },
      rules: {
        code: [
          {
            required: true,
            message: "请输入仓库编码",
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入仓库名称",
            trigger: "blur",
          },
        ],
        address: [
          {
            required: true,
            message: "请输入仓库地址",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入联系电话",
            trigger: "blur",
          },
          {
            pattern: /^1(3|4|5|7|8|9)\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        location: [
          {
            required: true,
            message: "请输入仓库位置",
            trigger: "blur",
          },
        ],
        wareTypeID: [
          {
            required: true,
            message: "请选择仓库类型",
            trigger: "change",
          },
        ],
        status: [
          {
            required: true,
            message: "请选择仓库状态",
            trigger: "change",
          },
        ],
        eRPStockNumber: [
          {
            required: true,
            message: "请选择ERP仓库编码",
            trigger: "change",
          },
        ],
        deptId: [
          {
            required: true,
            message: "请选择所属部门",
            trigger: "change",
          },
        ],
        stockStatus: [
          {
            required: true,
            message: "请选择库存状态",
            trigger: "change",
          },
        ],
      },
      //状态
      statusOptions: [],
      //仓库类型
      wareTypeIDOptions: [],
      //部门
      deptIdOptions: [],
      //库存状态
      stockStatusOptions: [],
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptIdOptions = response.data;
      });
    },

    async getDictText() {
      let res = await this.getDicts("sys_normal_disable");
      let resHouse = await this.getDicts("basic_housetype");
      let resstock = await this.getDicts("warehouse_stockstatus");
      if (res.data != null) {
        this.statusOptions = res.data;
      }

      if (resHouse.data != null) {
        this.wareTypeIDOptions = resHouse.data;
      }

      if (resstock.data != null) {
        this.stockStatusOptions = resstock.data;
      }
      this.$nextTick(() => {
        if (this.item) {
          this.formData = JSON.parse(JSON.stringify(this.item));
        }
      });
    },
    onOpen() {
      this.getDictText();
      this.getDeptTree();
    },
    onClose() {
      this.$refs["elForm"].resetFields();
      this.close();
    },
    close() {
      this.$emit("update:visible", false);
    },
    handleConfirm() {
      let form = JSON.parse(JSON.stringify(this.formData));
      console.log(form);
      this.$refs["elForm"].validate((valid) => {
        if (!valid) return;
        this.$emit("Ok", form);
        this.close();
      });
    },
  },
};
</script>
<style>
</style>
